var WN_DISTRICT = (function() {
	
	function loadDistrict(province, city, county, isWnManage) {
		var district = null;
		$.ajax({ 
	        type: "POST", 
	        url: (function() {
	        	return "invs_district.do/load";
	        })(),
	        dataType: "json",      
	        data: (function() {
	        	var p = {};
	        	if (province) p.province = province;
	        	if (city) p.city = city;
	        	if (county) p.county = county;
	        	return p;
	        })(),
	        async: false,
	        success:function(data) {
	        	if (data.errcode == 0) {
	        		district = data;
				} else {
					WeKnow.systemMsg('系统异常');
				}
	        },
	        error: function() {
	        	WeKnow.systemMsg('系统异常');
	        }
		});
		
		return district;
	}
	
	/**
	 * isWnManage :  null：wncrm  not null ：wn_manage
	 */
	function init($province, $city, $county, isWnManage) {
		var district = loadDistrict(
			$province.data('defaultvalue'),
			$city.data('defaultvalue'),
			$county.data('defaultvalue'),
			isWnManage
		);
		if (district) {
			$province.append((function() {
				var e = '<option value="">--省--</option>';
				if ('provinces' in district) {
					var defaultvalue = $province.data('defaultvalue');
					$(district.provinces).each(function(idx, p) {
						e += '<option value="' + p.id + '"';
						if (defaultvalue == p.id) {
							e += ' selected="selected"';
						}
						e += '>' + p.name + '</option>';
					});
				}
				return e;
			})());

			$city.append((function() {
				var e = '<option value="">--市--</option>';
				if ('citys' in district) {
					var defaultvalue = $city.data('defaultvalue');
					$(district.citys).each(function(idx, c) {
						e += '<option value="' + c.id + '"';
						if (defaultvalue == c.id) {
							e += ' selected="selected"';
						}
						e += '>' + c.name + '</option>';
					});
				}
				return e;
			})());
			
			if ($city.data('defaultvalue') && !$county.data('defaultvalue')) {
				$county.hide(); // 修改信息时，北京上海没有县
			} else {
				$county.append((function() {
					var e = '<option value="">--县--</option>';
					if ('countys' in district) {
						var defaultvalue = $county.data('defaultvalue');
						$(district.countys).each(function(idx, c) {
							e += '<option value="' + c.id + '"';
							if (defaultvalue == c.id) {
								e += ' selected="selected"';
							}
							e += '>' + c.name + '</option>';
						});
					}
					return e;
				})());
			}
		}
		
		$province.on('change', function() {
			var province = $(this).val();
			var district = loadDistrict(
				province,
				null,
				null,
				isWnManage
			);
			if (district) {
				$city.html((function() {
					var e = '<option value="">--市--</option>';
					if ('citys' in district) {
						$(district.citys).each(function(idx, c) {
							e += '<option value="' + c.id + '">' + c.name + '</option>';
						});
					}
					return e;
				})());
				$county.html((function() {
					var e = '<option value="">--县--</option>';
					return e;
				})());
			}
		});
		
		$city.on('change', function() {
			var city = $(this).val();
			var district = loadDistrict(
				null,
				city,
				null,
				isWnManage
			);
			if (district) {
				if ('countys' in district) {
					$county.show();
					$county.html((function() {
						var e = '<option value="">--县--</option>';
						$(district.countys).each(function(idx, c) {
							e += '<option value="' + c.id + '">' + c.name + '</option>';
						});
						return e;
					})());
				} else {
					$county.hide();
				}
			}
		});
	}
	
	return {
		init: init
	}
})();